package com.tf8.treasure.exception;

import com.tf8.treasure.enums.ReturnType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

public class RequestException extends RuntimeException {
	private static final Logger logger = LoggerFactory.getLogger(RequestException.class);
	private static final long serialVersionUID = 1887458L;
	private int errorCode;
	public RequestException() {
        super();
    }

	public RequestException(ReturnType type){
		super(type.getMsg());
		this.errorCode=type.getCode();
	}
    public RequestException(int errorCode, String message){
		super(message);
		this.errorCode=errorCode;
	}
    public RequestException(String message) {
        super(message);
    }
    public RequestException(String message, Throwable cause) {
        super(message, cause);
    }
    public RequestException(Throwable cause) {
        super(cause);
    }

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public void setMessage(String message){
	    try {
	    	Field field=null; 
	    	Class clazz =this.getClass();
	    	for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {  
	            try {  
	                field = clazz.getDeclaredField("detailMessage") ;  
	                break;  
	            } catch (Exception e) {
	            }   
	        }  
	    	field.setAccessible(true);//设置是否可以操作私有属性
	    	field.set(this, message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
